from spider_xia import SpiderManXia as SMX
from openpyxl import load_workbook,Workbook
import sys
import _thread
import time
import csv


class BrushBook(SMX):
    
    def __init__(self,url,regex,site_name,拼接,charset = 'gbk'):
        self.regex = regex
        self.site_name = site_name
        self.拼接 = 拼接
        try:    
            SMX.__init__(self,url,charset = 'gbk')

        except:#连接超时
            except_info = str(sys.exc_info())
            print(except_info)
            with open(
                '{}-错误.csv'.format(self.site_name),
                'a',
                newline = '',
                ) as csvfile:
                writer = csv.writer(csvfile)
                writer.writerow(['错误信息',except_info,'错误链接',self.url])
        

    def save_info_csv(self):
        info = self.find_info(self.regex)                       
        for i in range(len(info)):
            book_name = info[i][1]
            book_author = info[i][2]
            book_url = self.拼接+info[i][0]
            with open(
                    '{}-目录.csv'.format(self.site_name),
                    'a',
                    newline = '',
                    ) as csvfile:
                spamwriter = csv.writer(csvfile)
                spamwriter.writerow([book_name,book_author,book_url])
                
                
if __name__ == '__main__':


    
    regex = regex = '<p class="line"><a href="#">\[.*?\]</a><a href="(.*?)" class="blue">(.*?)</a>/<a href="/author/.*?" style="color:red">(.*?)</a>/月点击.*?</p>'
    site_name = 'm.biqugela.com'
    拼接 = 'http://m.biqugela.com'
    for i in range(1,5):
        url = 'http://m.biqugela.com/top-allvisit-{}/'.format(i)
        print(url)
        bb = BrushBook(url,regex,site_name,拼接)
        bb.save_info_csv()



